<template>
  <a-row :gutter="24" >
    <a-col :md="4" :sm="24">
      <a-card :bordered="false" :loading="treeLoading">
        <a-directory-tree
          :showIcon="false"
          :treeData="treeList"
          v-if="treeList.length"
          @select="handleClick"
          :defaultExpandAll="true"
          :replaceFields="replaceFields" />
      </a-card>
    </a-col>
    <a-col :md="19" :sm="24">
      <a-card :bordered="false">
        <div class="table-page-search-wrapper">
          <a-form layout="inline">
            <a-row :gutter="48">

              <a-col :md="6" :sm="24">
                <a-form-item label="网关名称">
                  <a-select style="width: 100%" v-model="queryParam.gatewayId" allow-clear placeholder="请选择网关名称" >
                    <a-select-option v-for="(item,ind) in gatewayList" :key="ind" :value="item.id" >{{ item.gatewayName }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>

              <a-col :md="6" :sm="24">
                <span class="table-page-search-submitButtons" style="padding-left: 16px;">
                  <a-form-item label="网关通道">
                  <a-input v-model="queryParam.gatewayChannel" allow-clear placeholder="请输入网关通道"/>
                </a-form-item>
                </span>
              </a-col>

              <a-col :md="6" :sm="24">
                <a-form-item label="处理状态">
                  <a-select style="width: 100%" v-model="queryParam.status" allow-clear placeholder="请选择处理状态" >
                    <a-select-option v-for="(item,ind) in statusList" :key="ind" :value="item.value" >{{ item.name }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>

              <a-col :md="!advanced && 6 || 24" :sm="24">
                <span class="table-page-search-submitButtons" style="padding-left: 16px;" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                  <a-button type="primary" class="btn_search_reset" @click="$refs.table.refresh()"><a-icon type="search" />查询</a-button>
                  <a-button style="margin-left: 8px" class="btn_search_reset" @click="() => {queryParam = {gatewayIds : gatewayIds.toString()};$refs.table.refresh()} "><a-icon type="reload" />重置</a-button>
                </span>
              </a-col>

            </a-row>
          </a-form>
        </div>

        <s-table
          ref="table"
          size="default"
          :columns="columns"
          :data="loadData"
          :alert="true"
          :rowKey="(record) => record.id"
        >
          <span slot="filePath" slot-scope="text,record">
            <a @click="$refs.playForm.play(record)">播放</a>
          </span>

          <span slot="status" slot-scope="text">
            {{ getStatus(text) }}
          </span>
        </s-table>
        <play-form ref="playForm" @ok="handleOk"/>
      </a-card>
    </a-col>
  </a-row>
</template>
<script>
  import { STable } from '@/components'
  import { page, del } from '@/api/modular/main/bizuploadqueuelog/bizUploadQueueLogManage'
  import playForm from './playForm'
  import { treeList } from '@/api/modular/main/bizstation/bizStationManage'
  import { gatewayList } from '@/api/modular/main/bizgateway/bizGatewayManage'

  export default {
    components: {
      STable,
      playForm,
    },

    data () {
      return {
        advanced: false, // 高级搜索 展开/关闭
        queryParam: {}, // 查询参数
        columns: [
          // 表头
/*          {
            title: '网关编号',
            dataIndex: 'gatewayCode',
            width: 50
          },*/
          {
            title: '网关名称',
            dataIndex: 'gatewayName'
          },
          {
            title: '网关通道',
            dataIndex: 'gatewayChannel',
            width: 50
          },
          {
            title: '文件',
            dataIndex: 'filePath',
            scopedSlots: { customRender: 'filePath' }
          },
          {
            title: '分数',
            dataIndex: 'score'
          },
          {
            title: '故障模型',
            dataIndex: 'accessToken'
          },
          {
            title: '分组',
            dataIndex: 'groupId'
          },
          {
            title: '状态',
            dataIndex: 'status',
            scopedSlots: { customRender: 'status' },
            width: 50,
            align: 'center'
          },
          {
            title: '创建日期',
            dataIndex: 'createdAt',
            width: 150,
            align: 'center'
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return page(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        selectedRowKeys: [],
        selectedRows: [],
        defaultExpandedKeys: [],
        replaceFields: {
          key: 'id'
        },
        treeList: [],
        gatewayList: [],
        statusList: [{
          name: '未处理',
          value: 0
        },{
          name: '处理中',
          value: 1
        },{
          name: '已处理',
          value: 2
        }],
        gatewayIds: [],
        stationId: 0,
        treeLoading: true
      }
    },

    created () {
      this.getStationTree()
    },

    methods: {
      del (record) {
        del(record).then((res) => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('删除失败：' + res.message)
          }
        }).catch((err) => {
          this.$message.error('删除错误：' + err.message)
        })
      },
      getStationTree () {
        treeList(this.queryParam).then(res => {
          this.treeLoading = false
          if (!res.success) {
            return
          }
          this.treeList = res.data
        })
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      handleClick (e) {
        this.queryParam = {}
        const param = {
          stationId: e.toString()
        }
        this.queryParam.gatewayIds = ''
        gatewayList(param).then(res => {
          this.gatewayIds = []
          this.gatewayList = res.data
          for (let i = 0; i < res.data.length; i++) {
            this.gatewayIds.push(res.data[i].id)
          }
          this.queryParam.gatewayIds = this.gatewayIds.toString()
          this.$refs.table.refresh()
        })
      },
      getStatus (text) {
        if (text === 0) {
          return '未处理'
        } else if (text === 1) {
          return '处理中'
        } else if (text === 2) {
          return '已处理'
        }
      }
    }
  }
</script>

<style lang="less" scoped>
@import '../~@/assets/css/list.less';

.table-operator {
  margin-bottom: 18px;
}

button {
  margin-right: 8px;
}
.table-page-search-wrapper{
  padding-left: 10px;
  /deep/ .ant-row{
    margin-left: 0!important;
  }
}
</style>
